Imports System.Io
Imports System.IO.IsolatedStorage
Imports System.Configuration
Imports System.Reflection
Imports Microsoft.Win32

Public Class Config
    Private Shared KeyCache As New Hashtable

    Public Sub New()
    End Sub

    Private ReadOnly Property RegPath() As String
        Get
            Dim hiveName As String = [Assembly].GetExecutingAssembly().GetName().Name.Replace(".", "\\")
            Return "SOFTWARE\\" & hiveName & "\\"
        End Get
    End Property

    Public Sub ResetCache()
        SyncLock Config.KeyCache
            Config.KeyCache.Clear()
        End SyncLock
    End Sub

    Public Sub SetKey(ByVal key As String, ByVal value As String)
        Try
            Dim k As RegistryKey = Registry.LocalMachine.CreateSubKey(RegPath)
            k.SetValue(key, value)
        Catch ex As Exception
            Try
                Dim k As RegistryKey = Registry.CurrentUser.CreateSubKey(RegPath)
                k.SetValue(key, value)
            Catch ex2 As Exception
            End Try
        End Try
    End Sub

    Public Function GetKey(ByVal key As String, ByVal defaultValue As String) As String
        Try
            Dim k As RegistryKey = Registry.LocalMachine.OpenSubKey(RegPath)
            Return CStr(k.GetValue(key, defaultValue))
        Catch ex As Exception
            Try
                Dim k As RegistryKey = Registry.CurrentUser.OpenSubKey(RegPath)
                Return CStr(k.GetValue(key, defaultValue))
            Catch ex2 As Exception
                Return defaultValue
            End Try
        End Try
    End Function
End Class
